#include <rtgui/rtgui.h>
#include "demo_items.h"

static void _rtgui_items_constructor(rtgui_items_t *items)
{
	if (!items) return;

	/* set default flag */
	items->flag = RTGUI_WIDGET_FLAG_DEFAULT;

	/* init list */
	rtgui_list_init(&(items->sibling));

	/* init gc */
	items->gc.foreground = default_foreground;
	items->gc.background = default_background;
	items->gc.font = rtgui_font_default();
	items->gc.textstyle = RTGUI_TEXTSTYLE_NORMAL;
	items->gc.textalign = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;
	items->align = RTGUI_ALIGN_LEFT | RTGUI_ALIGN_TOP;

	/* clear the garbage value of extent and clip */
	items->extent.x1 = items->extent.y1 = 0;
	items->extent.x2 = items->extent.y2 = 0;
	rtgui_region_init_with_extents(&items->clip, &items->extent);

	/* set parent and toplevel root */
	items->parent        = RT_NULL;
	items->toplevel      = RT_NULL;

	/* some common event handler */
	items->on_focus_in   = RT_NULL;
	items->on_focus_out  = RT_NULL;

#ifndef RTGUI_USING_SMALL_SIZE
	items->on_draw       = RT_NULL;
	items->on_mouseclick = RT_NULL;
	items->on_key        = RT_NULL;
	items->on_size       = RT_NULL;
	items->on_command    = RT_NULL;
#endif

	/* set default event handler */
	rtgui_object_set_event_handler(RTGUI_OBJECT(items), rtgui_items_event_handler);

	/* init user data private to 0 */
	items->user_data = 0;

	/* init clip information */
	rtgui_region_init(&(items->clip));

	/* init hardware dc */
	rtgui_dc_client_init(items);
}

/* Destroys the items */
static void _rtgui_items_destructor(rtgui_items_t *items)
{
	if (items == RT_NULL) return;

	if (items->parent != RT_NULL && RTGUI_IS_CONTAINER(items->parent))
	{
		/* remove items from parent's children list */
		rtgui_list_remove(&(RTGUI_CONTAINER(items->parent)->children), &(items->sibling));

		items->parent = RT_NULL;
	}

	/* fini clip region */
	rtgui_region_fini(&(items->clip));
}

DEFINE_CLASS_TYPE(items, "items",
	RTGUI_OBJECT_TYPE,
	_rtgui_items_constructor,
	_rtgui_items_destructor,
	sizeof(struct rtgui_items));
RTM_EXPORT(_rtgui_items);

rtgui_items_t *rtgui_items_create(rtgui_type_t *items_type)
{
	struct rtgui_items *items;

	items = RTGUI_WIDGET(rtgui_object_create(items_type));

	return items;
}
RTM_EXPORT(rtgui_items_create);

void rtgui_items_destroy(rtgui_items_t *items)
{
	rtgui_object_destroy(RTGUI_OBJECT(items));
}
RTM_EXPORT(rtgui_items_destroy);

rtgui_items_t *rtgui_items_get_next_sibling(rtgui_items_t *items)
{
	rtgui_items_t *sibling = RT_NULL;

	if (items->sibling.next != RT_NULL)
	{
		sibling = rtgui_list_entry(items->sibling.next, rtgui_items_t, sibling);
	}

	return sibling;
}
RTM_EXPORT(rtgui_items_get_next_sibling);

rtgui_items_t *rtgui_items_get_prev_sibling(rtgui_items_t *items)
{
	struct rtgui_list_node *node;
	rtgui_items_t *sibling, *parent;

	node = RT_NULL;
	sibling = RT_NULL;
	parent = items->parent;
	if (parent != RT_NULL)
	{
		rtgui_list_foreach(node, &(RTGUI_CONTAINER(parent)->children))
		{
			if (node->next == &(items->sibling))
				break;
		}
	}

	if (node != RT_NULL)
		sibling = rtgui_list_entry(node, rtgui_items_t, sibling);

	return sibling;
}
RTM_EXPORT(rtgui_items_get_prev_sibling);

void rtgui_items_set_parent(rtgui_items_t *items, rtgui_items_t *parent)
{
	/* set parent and toplevel items */
	items->parent = parent;
}
RTM_EXPORT(rtgui_items_set_parent);

rt_bool_t rtgui_items_event_handler(struct rtgui_object *object, rtgui_event_t *event)
{
	RTGUI_WIDGET_EVENT_HANDLER_PREPARE;

	switch (event->type)
	{
	case RTGUI_EVENT_SHOW:
		return rtgui_items_onshow(object, event);
	case RTGUI_EVENT_HIDE:
		return rtgui_items_onhide(object, event);
	case RTGUI_EVENT_UPDATE_TOPLVL:
		return rtgui_items_onupdate_toplvl(object, event);
#ifndef RTGUI_USING_SMALL_SIZE
	case RTGUI_EVENT_PAINT:
		if (items->on_draw != RT_NULL)
			return items->on_draw(RTGUI_OBJECT(items), event);
		break;

	case RTGUI_EVENT_KBD:
		if (items->on_key != RT_NULL)
			return items->on_key(RTGUI_OBJECT(items), event);
		break;

	case RTGUI_EVENT_MOUSE_BUTTON:
		if (items->on_mouseclick != RT_NULL)
			return items->on_mouseclick(RTGUI_OBJECT(items), event);
		break;

	case RTGUI_EVENT_COMMAND:
		if (items->on_command != RT_NULL)
			return items->on_command(RTGUI_OBJECT(items), event);
		break;

	case RTGUI_EVENT_RESIZE:
		if (items->on_size != RT_NULL)
			return items->on_size(RTGUI_OBJECT(items), event);
		break;
#endif
	default:
		return rtgui_object_event_handler(object, event);
	}

	return RT_FALSE;
}
RTM_EXPORT(rtgui_items_event_handler);

rt_bool_t rtgui_items_controller(struct rtgui_object *object, int cmd, void *args)
{	
	struct rtgui_items *items = RTGUI_ITEMS(object);

	RT_ASSERT(RT_NULL != items);

	switch(cmd)
	{
	case 0:
		break;
	default:
		break;
	}
	return RT_FALSE;
}



/*

*/
